﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TestingCenter.Common.KPIFormula;
using TestingCenter.Models;
using TestingCenterSystem.Models.PDP;

namespace KPIDemo
{
    /// <summary>
    /// 执行KPI方法类
    /// 自行实现
    /// 注意实现ICanAssembleFormula接口
    /// </summary>
    class KPIDemo 
    {
        int KpiId;
        public KPIDemo(int kpiid) {
            this.KpiId = kpiid;
        }
        /// <summary>
        /// 根据kpi id组织脚本算法
        /// </summary>
        /// <param name="kpiid"></param>
        /// <returns></returns>
        public KPIFormulaViewModel AssembleFormula()
        {
            try
            {
                //根据SD_EKPI_ID获取方法体与参数
                //组织方法体
                SD_EKPI_FORMULA body = new SD_EKPI_FORMULA() { SD_EKPI_ID = 10154, NUM_FORMULA = "1", FRA_FORMULA = "1 if(P1-P2).TotalMinutes<=15 else 0" };
                //组织参数
                List<SD_EKPI_PARAM> list = new List<SD_EKPI_PARAM>() {
                    new SD_EKPI_PARAM() {SD_EKPI_ID=10154,SD_ITEM_ID=10200,SD_EKPI_PARAM_NAME="P1" },
                new SD_EKPI_PARAM() {SD_EKPI_ID=10154,SD_ITEM_ID=10201,SD_EKPI_PARAM_NAME="P2" }};
                return new KPIFormulaViewModel(body, list);
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        public object ExeKPI()
        {
            try
            {
                //初始化python引擎
                UsingPython up = new UsingPython(AssembleFormula());
                //按照配置EP_KPI_PARAM中KPI的配置组织参数
                List<SimpleParamViewModel> listp = new List<SimpleParamViewModel>() {
                new SimpleParamViewModel() {Code="P2",DataType="datetime",Value="2017/7/6 17:09:25" },
                new SimpleParamViewModel() {Code="P1",DataType="datetime",Value="2017/7/1 17:19:25" },
            };
                return up.ExcuteScriptFile(listp);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
           
        }
    }
}
